package de.dqi.pong;

import java.text.DecimalFormat;

import de.dqi.pong.engine.InitializationNotification;
import de.dqi.pong.menu.Frame;

/**
 * TODO Formatter - finish class definition
 * <hr>
 * 
 * <hr><dl><dt>
 * Summary: </dt><br><dd>
 *  
 * </dd><br><dt>
 * Author: </dt><br><dd>
 *  Vincent
 * </dd></dl>
 */
public class Formatter {
	
	public static String format(double value) {
		return format(value, 3, 3);
	}
	
	public static String format(double value, int head, int tail) {
		if (head < 0) {
			head = 0;
		}
		if (tail < 0) {
			tail = 0;
		}
		
		boolean negative = (value < 0);
		boolean clipped = false;
		value = Math.abs(value);
		String text = Double.toString(value);
		int dot = text.indexOf('.');
		
		String pre = text.substring(0, dot);
		String post = text.substring(dot + 1, text.length());
		
		if (pre.length() < head) {
			String temp = "";
			for (int i = head - pre.length(); i > 0; i--) {
				temp = temp + " ";
			}
			pre = temp + pre;
		} else if (pre.length() > head) {
			clipped = true;
			pre = pre.substring(pre.length() - head, pre.length());
		}
		
		if (post.length() < tail) {
			String temp = "";
			for (int i = tail - post.length(); i > 0; i--) {
				temp = temp + " ";
			}
			post = post + temp;
		} else if (post.length() > tail) {
			post = post.substring(0, tail);
		}
		
		if (tail == 0) {
			text = pre;
		} else {
			text = pre + "," + post;
		}
		
		String prefix = " ";
		
		if (clipped) {
			if (negative) {
				prefix = "<";
			} else {
				prefix = ">";
			}
		} else if (negative) {
			prefix = "-";
		}
		
		return prefix + text;
	}
}
